<?php

namespace Wonfu\Core\Database;

use Cake\Database\Log\LoggingStatement;
use Wonfu\Core\Database\CakeLogHookInterface;

class CakeLoggingStatementWrapper extends LoggingStatement
{

    /**
     * @var CakeLogHookInterface
     */
    protected $logHook;

    /**
     * Copies the logging data to the passed LoggedQuery and sends it
     * to the logging system.
     *
     * @return void
     */
    protected function _log(): void
    {
        if ($this->loggedQuery === null) {
            return;
        }

        $this->loggedQuery->query = $this->queryString;
        $this->getLogger()->info((string)$this->loggedQuery,
            ['took' => $this->loggedQuery->took, 'numRows' => $this->loggedQuery->numRows]);
        if($this->logHook){
            ($this->logHook)($this->loggedQuery);
        }
        $this->loggedQuery = null;
    }

    /**
     * @param CakeLogHookInterface $logHook
     */
    public function setLogHook(CakeLogHookInterface $logHook): void
    {
        $this->logHook = $logHook;
    }

    /**
     * @return CakeLogHookInterface
     */
    public function getLogHook(): CakeLogHookInterface
    {
        return $this->logHook;
    }








}